/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-04-04
 * Time: 13:05
 */
public class round_robinQueue<E> {
    public Object[] elem;//数组
    public int front;//队头
    public int rear;//队尾

    public round_robinQueue(int k) {
        this.elem = new Object[k + 1];
    }

    //入队一个元素
    public boolean offer(int value) {
        //满了不能插
        if (isFull()) {
            return false;
        }
        elem[rear] = value;
        rear = (rear + 1) % elem.length;
        return true;
    }

    //出队一个元素
    public boolean poll() {
        if (isEmpty()) {
            return false;
        }
        front = (front + 1) % elem.length;
        return true;
    }

    //获取队头元素
    public E getFront() {
        if (isEmpty()) {
            return null;
        }
        return (E) elem[front];
    }

    //获取队尾元素
    public E Rear() {
        if (isEmpty()) {
            return null;
        }
        //rear指向的是下一个位置,不是最后一个元素,如果rear=0,会越界
        if (rear == 0) {
            return (E) elem[elem.length - 1];
        }
        return (E) elem[rear - 1];
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
        return (rear + 1) % elem.length == front;
    }
}
